﻿using FireflySoft.RateLimit.Core;
using System.Collections.Generic;
using System.Linq;

namespace FireflySoft.RateLimit.AspNetCore
{
    /// <summary>
    /// multiple algorithm manager
    /// </summary>
    public static class MultiAlgorithmManager
    {
        private static readonly Dictionary<string, IAlgorithm> _algorithmDict = new Dictionary<string, IAlgorithm>();

        /// <summary>
        /// set algorithms
        /// </summary>
        /// <param name="algorithms"></param>
        public static void SetAlgorithmInstances(IAlgorithm[] algorithms)
        {
            for (int i = 0; i <= algorithms.Length - 1; i++)
            {
                _algorithmDict.Add(algorithms[i].GetType().ToString(), algorithms[i]);
            }
        }


        /// <summary>
        /// get algorithm by name
        /// </summary>
        /// <param name="algorithmName"></param>
        /// <returns></returns>
        public static IAlgorithm GetAlgorithmInstance(string algorithmName)
        {
            if (_algorithmDict.ContainsKey(algorithmName))
                return _algorithmDict[algorithmName];
            return null;
        }

        /// <summary>
        /// get all algorithms
        /// </summary>
        /// <returns></returns>
        public static IAlgorithm[] GetAlgorithmInstances()
        {
            return _algorithmDict.Values.ToArray();
        }
    }
}
